<template>
  <!-- 生成货运单-->
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="addShipmentOrder"
      :isMemoryPagination = "true"
      :isSelectColumn="false"
      height='auto'
      :onColumnsChange="(columns) => (this.columns = columns)"
    >
    </FilterTable>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)" :disabled="ischange">保存</multiuse-button>
      <el-button @click="closeDrawer(1)">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import { createShipmentOrder } from '@/api/omSale/OrderManagement/ShipmentOrderQuery';
import { IDNumber, phone } from '@/utils/validate';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
export default {
  name: 'addShipmentOrder',
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      columns: this.createdTable(),
      ischange: false,
      tableList: []
    };
  },
  components: {},
  props: ['fromData'],
  mounted() {
    console.log('传入参数',this.fromData);
    this.formList.find((x) => x.fieldName == 'dealerName').initialValue = this.fromData.dealerName;
    this.formList.find((x) => x.fieldName == 'address').initialValue = this.fromData.address;
    this.formList.find((x) => x.fieldName == 'deliveryCityName').initialValue = this.fromData.deliveryCityName;
    this.formList.find((x) => x.fieldName == 'linkMan').initialValue = this.fromData.linkMan;
    this.tableList = this.fromData.orderList;
    this.$refs.table.SET_TABLE_DATA(this.fromData.orderList);
  },
  mixins: [dictionary],
  methods: {
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        //生成货运单
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          json.addressId = this.fromData.addressId;
          json.dealerCode = this.fromData.dealerCode;
          json.orderList = this.tableList;
          console.log('生成货运单', json);
          this.ischange = true;
          let res = await createShipmentOrder(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '货运单创建成功',
              type: 'success'
            });
            this.ischange = false;
            this.$emit('close', this.saveData);
          } else {
            this.$emit('close');
            this.saveData = null;
          }
        }
      }
    },
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '货运信息',
        },
        {
          type: 'INPUT',
          label: '货运单号',
          fieldName: 'shipmentOrderNo',
          disabled: true,
        },
         {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '收车地址',
          fieldName: 'address',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '送达城市',
          fieldName: 'deliveryCityName',
          disabled: true,        
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'linkMan',
          disabled: true,
        },
        {
          type: 'DATE',
          label: '发车时间',
          fieldName: 'carrierDate',
          placeholder: '请输入',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          dateType: 'datetime',
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '发车基地',
          fieldName: 'carrierBase',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '物流公司',
          fieldName: 'carrierCompany',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'trailerBrand',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '驾驶员姓名',
          fieldName: 'carrierDriver',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '驾驶员电话',
          fieldName: 'carrierDriverPhone',
          placeholder: '请输入',
          rules: [
            { required: true, message: '请输入', trigger: 'change' },
            { message: '请输入正确的手机号', validator: phone, trigger: 'blur' }
            ]
        },
        {
          type: 'INPUT',
          label: '驾驶员身份证',
          fieldName: 'carrierDriverIdCard',
          placeholder: '请输入',
          rules: [
            { required: true, message: '请输入', trigger: 'change' },
            { message: '请输入正确的身份证号', validator: IDNumber, trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '板次',
          fieldName: 'frequency',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'change' }]
        },
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        }
      ];
    },
     createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '电池厂家',
          dataIndex: 'batteryOem',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'SAP销售单号',
          dataIndex: 'sapNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        }
      ];
    }
  },
};
</script>
